package com.xiaoyu.awakening.ai.search.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.xiaoyu.awakening.ai.search.config.properties.AwkSearchConfigProperties;
import com.xiaoyu.awakening.ai.search.core.intention.IAwkIntentionDetect;
import com.xiaoyu.awakening.ai.search.model.AwkIntentionDetect;
import com.xiaoyu.awakening.ai.search.model.vo.SearchRequestVO;
import com.xiaoyu.awakening.ai.search.service.IAwkIntentionService;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 意图识别服务
 *
 * @author wangxiaoyu
 * @date 2025/8/6 15:24
 * @since v1.0
 */
@Service
public class AwkIntentionServiceImpl implements IAwkIntentionService {
    @Resource
    private ChatClient intentionAgent;
    @Resource
    private ChatClient translateAgent;
    @Resource
    private AwkSearchConfigProperties awkSearchConfigProperties;
    private final List<IAwkIntentionDetect> intentionHandlerList;

    @Autowired
    public AwkIntentionServiceImpl(List<IAwkIntentionDetect> intentionHandlerList) {
        this.intentionHandlerList = intentionHandlerList;
    }

    @Override
    public List<AwkIntentionDetect> detect(SearchRequestVO searchRequestVO) {
        //标准化query
        String normalizedQuery = doNormalizedQ(searchRequestVO.getQ());
        searchRequestVO.setNormalizedQ(StrUtil.isBlank(normalizedQuery) ? searchRequestVO.getQ() : normalizedQuery);

        //扩展query
        List<String> extendedQueryList = doExtendedQ(searchRequestVO, awkSearchConfigProperties.getIntentionProperties().getEnableExtendFromHistory(),
                awkSearchConfigProperties.getIntentionProperties().getEnableNormalized());

        //意图识别
        List<AwkIntentionDetect> intentionDetectList = new ArrayList<>();
        extendedQueryList.forEach(extendedQuery -> {
            List<AwkIntentionDetect> detect = intentionHandlerList.parallelStream()
                    .map(intentionHandler -> intentionHandler.detect(searchRequestVO, extendedQuery)).flatMap(List::stream).toList();
            if (CollectionUtil.isNotEmpty(detect)) {
                intentionDetectList.addAll(detect);
            }
        });

        //意图评估筛选
        doEvalIntentionAndChoose(searchRequestVO, intentionDetectList, awkSearchConfigProperties.getIntentionProperties().getIntentionChooseNum(),
                awkSearchConfigProperties.getIntentionProperties().getEnableBalanceMode());
        return intentionDetectList;
    }


    /**
     * 标准化query
     *
     * @param query 原始query
     * @return 标准化后的query
     */
    private String doNormalizedQ(String query) {
        // 查询翻译
        return query;
    }

    /**
     * 扩展query
     *
     * @param searchRequestVO         搜索请求VO
     * @param enableExtendFromHistory 是否从历史中扩展query
     * @param enableNormalized        是否标准化扩展后的query
     * @return 扩展后的query列表
     */
    private List<String> doExtendedQ(SearchRequestVO searchRequestVO, Boolean enableExtendFromHistory, Boolean enableNormalized) {
        List<String> queryList = new ArrayList<>();
        queryList.add(searchRequestVO.getNormalizedQ());

        //todo:LLM扩展query
        String extendedQuery = intentionAgent.prompt().user(searchRequestVO.getQ()).call().content();
        queryList.add(extendedQuery);

        //todo:从历史中扩展query
        //标准化
        if (enableNormalized) {
            queryList.forEach(query -> {
                String normalizedQuery = doNormalizedQ(query);
                if (StrUtil.isNotBlank(normalizedQuery)) {
                    queryList.add(normalizedQuery);
                }
            });
        }
        return queryList;
    }

    /**
     * 意图评估筛选
     *
     * @param intentionDetectList 意图识别结果列表
     * @param top                 topN
     * @param enableBalanceMode   是否开启平衡模式：开启后，会根据意图的置信度进行加权，置信度高的意图会被优先选择
     */
    private void doEvalIntentionAndChoose(SearchRequestVO searchRequestVO, List<AwkIntentionDetect> intentionDetectList, Integer top, Boolean enableBalanceMode) {
    }


}
